/**
 * 
 */
package com.jdy.haoduoai.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdy.haoduoai.activity.PainPointDiscussionActivity;
import com.jdy.haoduoai.model.PainPointItem;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * @author zp 2016-11-29 上午9:59:38
 */
public class PainPointAdapter extends BaseAdapter {

  private final Context mContext;

  private final LayoutInflater mInflater;

  private final SparseArray<View> mSpArray;

  private final ArrayList<PainPointItem> mPainPointItemList;

  private OnPainPointSelectListener mOnPainPointSelectListener;

  private ArrayList<Integer> mSelectedList;

  public PainPointAdapter(Context ctx, ArrayList<PainPointItem> list) {
    this.mContext = ctx;

    this.mPainPointItemList = list;

    this.mInflater = LayoutInflater.from(mContext);

    this.mSpArray = new SparseArray<View>();

    this.mSelectedList = new ArrayList<Integer>();

  }

  public void setSelectedList(ArrayList<Integer> list) {
    this.mSelectedList = list;
  }

  public void setPainPointSelectListener(OnPainPointSelectListener listener) {
    this.mOnPainPointSelectListener = listener;
  }

  @Override
  public int getCount() {
    if (mPainPointItemList != null)
      return mPainPointItemList.size();
    else
      return 0;
  }

  @Override
  public PainPointItem getItem(int position) {
    if (mPainPointItemList != null)
      return mPainPointItemList.get(position);
    else
      return null;
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    if (mPainPointItemList != null)
      return position;
    else
      return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    final PainPointItem ppi = mPainPointItemList.get(position);
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.painpoint_item_layout, null);
      holder.touchAreaLayout =  convertView
          .findViewById(R.id.touch_area_layout);
      holder.backgroundImg =  convertView
          .findViewById(R.id.painpoint_img);
      holder.nameTv =  convertView.findViewById(R.id.name_tv);
      holder.descriptionTv =  convertView.findViewById(R.id.desc_tv);
      holder.timeLengthTv =  convertView
          .findViewById(R.id.timelength_tv);
      holder.numberTv =  convertView
          .findViewById(R.id.participate_number_tv);
      holder.selectCb =  convertView.findViewById(R.id.select_cb);
      holder.selectLayout =  convertView
          .findViewById(R.id.select_layout);

      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    final ViewHolder holder2 = holder;

    holder.descriptionTv.setText(ppi.desc_short);

    holder.nameTv.setText(ppi.name);

    holder.numberTv.setText("已有" + ppi.user_number + "人参加");

    Picasso.with(mContext).load(ppi.img).into(holder.backgroundImg);

    holder.selectCb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if (mOnPainPointSelectListener != null) {
          mOnPainPointSelectListener.onPainPointSelect(ppi.id, isChecked);
        }
        ppi.selected = isChecked;
      }
    });

    holder.selectCb.setChecked(ppi.selected);

    holder.touchAreaLayout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

        // if (!Tools.isFastDoubleClick()) {
        Intent t = new Intent(mContext, PainPointDiscussionActivity.class);
        t.putExtra("name", ppi.name);
        t.putExtra("number", ppi.user_number);
        t.putExtra("img", ppi.img);
        t.putExtra("desc", ppi.desc);
        t.putExtra("desc_short", ppi.desc_short);
        t.putExtra("habit_id", ppi.id);
        t.putExtra("selected", ppi.selected);
        Bundle extraBundle = new Bundle();
        extraBundle.putIntegerArrayList("selected_list", mSelectedList);
        t.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        t.putExtras(extraBundle);
        ((Activity) mContext).startActivityForResult(t, 111);
        // }

      }
    });

    holder.selectLayout.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        holder2.selectCb.performClick();
      }
    });

    return convertView;
  }

  private class ViewHolder {
    ImageView backgroundImg;

    RelativeLayout touchAreaLayout;

    TextView nameTv;

    TextView descriptionTv;

    TextView timeLengthTv;

    TextView numberTv;

    CheckBox selectCb;

    RelativeLayout selectLayout;
  }

}
